package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_mass_mailing_campaign;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_mass_mailing_campaignSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_mass_mailing_campaignService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[群发邮件营销] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_mass_mailing_campaignServiceImpl implements IMail_mass_mailing_campaignService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_mass_mailing_campaignClientService mail_mass_mailing_campaignClientService;

    @Override
    public Mail_mass_mailing_campaign get(Integer id) {
        Imail_mass_mailing_campaign clientModel = mail_mass_mailing_campaignClientService.createModel();
        clientModel.setId(id);
        mail_mass_mailing_campaignClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_mass_mailing_campaign get(Mail_mass_mailing_campaign mail_mass_mailing_campaign) {
        Mail_mass_mailing_campaign retmail_mass_mailing_campaign = this.get(mail_mass_mailing_campaign.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_mass_mailing_campaign.getClass(), mail_mass_mailing_campaign.getClass(), false);
        copier.copy(retmail_mass_mailing_campaign, mail_mass_mailing_campaign, null);
        return mail_mass_mailing_campaign ;
    }

    @Override
    public void createBatch(List<Mail_mass_mailing_campaign> mail_mass_mailing_campaigns, int batchSize) {

                        //mail_mass_mailing_campaignClientService.createBatch(mail_mass_mailing_campaigns,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_mass_mailing_campaignClientService.removeBatch(mail_mass_mailing_campaigns) ;

    }

    @Override
    public void updateBatch(List<Mail_mass_mailing_campaign> mail_mass_mailing_campaigns, int batchSize) {

        //mail_mass_mailing_campaignClientService.updateBatch(mail_mass_mailing_campaigns,batchSize) ;

    }

    @Override
    public boolean create(Mail_mass_mailing_campaign mail_mass_mailing_campaign) {
        boolean bOk = false ;
        Imail_mass_mailing_campaign clientModel = this.convert2Model(mail_mass_mailing_campaign,null);
        mail_mass_mailing_campaignClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_mass_mailing_campaign);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Mail_mass_mailing_campaign mail_mass_mailing_campaign) {
        boolean bOk = false ;
        Imail_mass_mailing_campaign clientModel = this.convert2Model(mail_mass_mailing_campaign,null);
        mail_mass_mailing_campaignClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_mass_mailing_campaign) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_mass_mailing_campaign clientModel = mail_mass_mailing_campaignClientService.createModel();
        clientModel.setId(id);
        mail_mass_mailing_campaignClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_mass_mailing_campaign> searchDefault(Mail_mass_mailing_campaignSearchContext context) {
        List<Mail_mass_mailing_campaign> list = new ArrayList<Mail_mass_mailing_campaign>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign> clientModelList = mail_mass_mailing_campaignClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_campaign>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据群发邮件营销阶段查询
     */
    @Override
    public Page<Mail_mass_mailing_campaign> selectByOdoo_stage(Integer id){
        Mail_mass_mailing_campaignSearchContext context = new Mail_mass_mailing_campaignSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_campaign> list = new ArrayList<Mail_mass_mailing_campaign>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign> clientModelList = mail_mass_mailing_campaignClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_campaign>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mass_mailing_campaign> selectByOdoo_create(Integer id){
        Mail_mass_mailing_campaignSearchContext context = new Mail_mass_mailing_campaignSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_campaign> list = new ArrayList<Mail_mass_mailing_campaign>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign> clientModelList = mail_mass_mailing_campaignClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_campaign>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mass_mailing_campaign> selectByOdoo_user(Integer id){
        Mail_mass_mailing_campaignSearchContext context = new Mail_mass_mailing_campaignSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_campaign> list = new ArrayList<Mail_mass_mailing_campaign>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign> clientModelList = mail_mass_mailing_campaignClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_campaign>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mass_mailing_campaign> selectByOdoo_write(Integer id){
        Mail_mass_mailing_campaignSearchContext context = new Mail_mass_mailing_campaignSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_campaign> list = new ArrayList<Mail_mass_mailing_campaign>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign> clientModelList = mail_mass_mailing_campaignClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_campaign>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据UTM 营销活动查询
     */
    @Override
    public Page<Mail_mass_mailing_campaign> selectByOdoo_campaign(Integer id){
        Mail_mass_mailing_campaignSearchContext context = new Mail_mass_mailing_campaignSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_campaign> list = new ArrayList<Mail_mass_mailing_campaign>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign> clientModelList = mail_mass_mailing_campaignClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_campaign>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据UTM媒体查询
     */
    @Override
    public Page<Mail_mass_mailing_campaign> selectByOdoo_medium(Integer id){
        Mail_mass_mailing_campaignSearchContext context = new Mail_mass_mailing_campaignSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_campaign> list = new ArrayList<Mail_mass_mailing_campaign>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign> clientModelList = mail_mass_mailing_campaignClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_campaign>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据UTM来源查询
     */
    @Override
    public Page<Mail_mass_mailing_campaign> selectByOdoo_source(Integer id){
        Mail_mass_mailing_campaignSearchContext context = new Mail_mass_mailing_campaignSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_campaign> list = new ArrayList<Mail_mass_mailing_campaign>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign> clientModelList = mail_mass_mailing_campaignClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_campaign clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_campaign>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_mass_mailing_campaign convert2Model(Mail_mass_mailing_campaign domain , Imail_mass_mailing_campaign model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_mass_mailing_campaignClientService.createModel() ;
        }

         if(domain.getTotalDirtyFlag())
            model.setTotal(domain.getTotal());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getMass_mailing_idsDirtyFlag())
            model.setMass_mailing_ids(domain.getMass_mailing_ids());
         if(domain.getOpened_ratioDirtyFlag())
            model.setOpened_ratio(domain.getOpened_ratio());
         if(domain.getFailedDirtyFlag())
            model.setFailed(domain.getFailed());
         if(domain.getScheduledDirtyFlag())
            model.setScheduled(domain.getScheduled());
         if(domain.getBouncedDirtyFlag())
            model.setBounced(domain.getBounced());
         if(domain.getClicks_ratioDirtyFlag())
            model.setClicks_ratio(domain.getClicks_ratio());
         if(domain.getSentDirtyFlag())
            model.setSent(domain.getSent());
         if(domain.getReceived_ratioDirtyFlag())
            model.setReceived_ratio(domain.getReceived_ratio());
         if(domain.getIgnoredDirtyFlag())
            model.setIgnored(domain.getIgnored());
         if(domain.getUnique_ab_testingDirtyFlag())
            model.setUnique_ab_testing(domain.getUnique_ab_testing());
         if(domain.getRepliedDirtyFlag())
            model.setReplied(domain.getReplied());
         if(domain.getTotal_mailingsDirtyFlag())
            model.setTotal_mailings(domain.getTotal_mailings());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getBounced_ratioDirtyFlag())
            model.setBounced_ratio(domain.getBounced_ratio());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getTag_idsDirtyFlag())
            model.setTag_ids(domain.getTag_ids());
         if(domain.getOpenedDirtyFlag())
            model.setOpened(domain.getOpened());
         if(domain.getReplied_ratioDirtyFlag())
            model.setReplied_ratio(domain.getReplied_ratio());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDeliveredDirtyFlag())
            model.setDelivered(domain.getDelivered());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getMedium_id_textDirtyFlag())
            model.setMedium_id_text(domain.getMedium_id_text());
         if(domain.getStage_id_textDirtyFlag())
            model.setStage_id_text(domain.getStage_id_text());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getSource_id_textDirtyFlag())
            model.setSource_id_text(domain.getSource_id_text());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getCampaign_idDirtyFlag())
            model.setCampaign_id(domain.getCampaign_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getStage_idDirtyFlag())
            model.setStage_id(domain.getStage_id());
         if(domain.getSource_idDirtyFlag())
            model.setSource_id(domain.getSource_id());
         if(domain.getMedium_idDirtyFlag())
            model.setMedium_id(domain.getMedium_id());
        return model ;
    }

    public Mail_mass_mailing_campaign convert2Domain( Imail_mass_mailing_campaign model ,Mail_mass_mailing_campaign domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_mass_mailing_campaign();
        }

        if(model.getTotalDirtyFlag())
            domain.setTotal(model.getTotal());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getMass_mailing_idsDirtyFlag())
            domain.setMass_mailing_ids(model.getMass_mailing_ids());
        if(model.getOpened_ratioDirtyFlag())
            domain.setOpened_ratio(model.getOpened_ratio());
        if(model.getFailedDirtyFlag())
            domain.setFailed(model.getFailed());
        if(model.getScheduledDirtyFlag())
            domain.setScheduled(model.getScheduled());
        if(model.getBouncedDirtyFlag())
            domain.setBounced(model.getBounced());
        if(model.getClicks_ratioDirtyFlag())
            domain.setClicks_ratio(model.getClicks_ratio());
        if(model.getSentDirtyFlag())
            domain.setSent(model.getSent());
        if(model.getReceived_ratioDirtyFlag())
            domain.setReceived_ratio(model.getReceived_ratio());
        if(model.getIgnoredDirtyFlag())
            domain.setIgnored(model.getIgnored());
        if(model.getUnique_ab_testingDirtyFlag())
            domain.setUnique_ab_testing(model.getUnique_ab_testing());
        if(model.getRepliedDirtyFlag())
            domain.setReplied(model.getReplied());
        if(model.getTotal_mailingsDirtyFlag())
            domain.setTotal_mailings(model.getTotal_mailings());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getBounced_ratioDirtyFlag())
            domain.setBounced_ratio(model.getBounced_ratio());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getTag_idsDirtyFlag())
            domain.setTag_ids(model.getTag_ids());
        if(model.getOpenedDirtyFlag())
            domain.setOpened(model.getOpened());
        if(model.getReplied_ratioDirtyFlag())
            domain.setReplied_ratio(model.getReplied_ratio());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDeliveredDirtyFlag())
            domain.setDelivered(model.getDelivered());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getMedium_id_textDirtyFlag())
            domain.setMedium_id_text(model.getMedium_id_text());
        if(model.getStage_id_textDirtyFlag())
            domain.setStage_id_text(model.getStage_id_text());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getSource_id_textDirtyFlag())
            domain.setSource_id_text(model.getSource_id_text());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getCampaign_idDirtyFlag())
            domain.setCampaign_id(model.getCampaign_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getStage_idDirtyFlag())
            domain.setStage_id(model.getStage_id());
        if(model.getSource_idDirtyFlag())
            domain.setSource_id(model.getSource_id());
        if(model.getMedium_idDirtyFlag())
            domain.setMedium_id(model.getMedium_id());
        return domain ;
    }


}
